package day15;

class allPlayer {
    // 静态内部类用于持有球员对象的引用
    private static class PlayerHolder {
        // 使用一个数组来存储最多11个球员对象
        private static final allPlayer[] PLAYERS = new allPlayer[11];
        private static int currentIndex = 0;

        // 私有构造函数防止实例化
        private PlayerHolder() {}

        // 静态方法用于获取球员对象
        public static allPlayer getPlayer(String name, int number) {
            if (currentIndex >= PLAYERS.length) {
                throw new IllegalStateException("Cannot create more than 11 players");
            }
            PLAYERS[currentIndex++] = new allPlayer(name, number);
            return PLAYERS[currentIndex - 1];
        }
    }

    // 私有构造函数防止外部直接实例化
    private String name;
    private int number;

    private allPlayer(String name, int number) {
        this.name = name;
        this.number = number;
    }

    // 公共的静态工厂方法用于获取球员对象
    public static allPlayer createPlayer(String name, int number) {
        return PlayerHolder.getPlayer(name, number);
    }

    // Getter方法
    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", number=" + number +
                '}';
    }

    // 主方法用于测试
    public static void main(String[] args) {
        try {
            for (int i = 1; i <= 11; i++) {
                allPlayer player = allPlayer.createPlayer("Player" + i, i);
                System.out.println(player);
            }
            // 尝试创建第12个球员，应该会抛出异常
            allPlayer extraPlayer = allPlayer.createPlayer("ExtraPlayer", 12);
        } catch (IllegalStateException e) {
            System.err.println(e.getMessage());
        }
    }
}